//
//  JiFenCycleViewController.m
//  1243
//
//  Created by 杨森 on 16/5/5.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "JiFenCycleViewController.h"
#import "PieChartView.h"
#import "ReadLoginData.h"
#import "NetWorkTools.h"

#import "JFButton.h"

#import "NYWheel.h"
#define NORMWIDTHA [[UIScreen mainScreen] bounds].size.width / 414
#define NORMHEIGHTA [[UIScreen mainScreen] bounds].size.height / 736
@interface JiFenCycleViewController ()<PieChartViewDelegate,PieChartViewDataSource,UIAlertViewDelegate>
{
    PieChartView *pieChartView;
    //    CGFloat angle;
    NSString *strPrise;
    NSTimer *time;
    NSString *temp;
    NSString *num;
    NSString *messange;
}
@property (nonatomic, strong) CADisplayLink *link;

@property(nonatomic,weak)UIButton *wheelButton;
@property(nonatomic,strong)NSArray *wheelArray;
@property (nonatomic,copy)NSString *str;
@property(nonatomic,weak)UILabel *topLabel;
@property(nonatomic,weak)UILabel *UserLabel;
@property (nonatomic, weak) NYWheel *wheel;
@property(nonatomic,assign)int randomNum;
@property (nonatomic,strong)UIImageView *image;
@property (nonatomic, strong)NSArray *arr;

@end

@implementation JiFenCycleViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    //    angle = 0;
    
    [self addViews];
    //    [self netWorkTools];
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    [self netWorkGet];
    [self shareNetWork];
}

- (void)addViews
{
    
    
    self.view.backgroundColor = COLOR(70, 139, 184, 1);
    self.navigationController.navigationBar.translucent = YES;
    UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(refreshPropertyList)];
    self.navigationItem.leftBarButtonItem = anotherButton;
    //背景
    UIImageView *bgWheelView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg"]];
    CGFloat bgWheelW = WEITH/6*5;
    bgWheelView.frame = CGRectMake(5, HEIGHT/3,WIDTH-10*NORMWIDTHA,bgWheelW+50*NORMHEIGHTA);
    
    //圈圈
    CGFloat wheelW = WEITH/2;
    NYWheel *wheel = [NYWheel wheel];
    wheel.arr = self.arr;
  //  wheel.frame = CGRectMake(WEITH/4-40*NORMWIDTHA, HEIGHT/2.35-15*NORMHEIGHTA, wheelW, wheelW);
    //
    
   [self.view addSubview:wheel];
    [self.view addSubview:bgWheelView];
    wheel.center  =    bgWheelView.center;
    self.wheel = wheel;
    
    
    //按钮
    
    UIButton *wheelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [wheelBtn setBackgroundImage:[UIImage imageNamed:@"arrow_ks"] forState:UIControlStateNormal];
    wheelBtn.frame = CGRectMake(0, HEIGHT/2.35- 50*NORMHEIGHTA +wheelW/2, 150*NORMWIDTHA, 150*NORMHEIGHTA);
    [self.view addSubview:wheelBtn];
    [wheelBtn setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
    [wheelBtn addTarget:self action:@selector(didWheelBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [wheelBtn setTitle:@"开始" forState:UIControlStateNormal];
    self.wheelButton = wheelBtn;
    wheelBtn.center = bgWheelView.center;
    //38 81 117
   
    UILabel *topLabelA = [[UILabel alloc] initWithFrame:CGRectMake(0,64, WEITH, 44*NORMHEIGHTA)];
    topLabelA.textAlignment = NSTextAlignmentCenter;
    topLabelA.text = @"请您点击按钮抽奖";
    topLabelA.backgroundColor = COLOR(38,81, 117, 1);
    topLabelA.textColor = [UIColor whiteColor];
    topLabelA.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:topLabelA];
    self.UserLabel = topLabelA;
    UILabel *topLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,64+ 44*NORMHEIGHTA, WEITH, 44*NORMHEIGHTA)];
    topLabel.textAlignment = NSTextAlignmentCenter;
    topLabel.text = @"";
    topLabel.backgroundColor = COLOR(38,81, 117, 1);
    topLabel.textColor = [UIColor whiteColor];
    topLabel.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:topLabel];
    self.topLabel = topLabel;
    
}

- (void)netWorkGet{
    
    NSDictionary *params = @{
                             @"MenthodParms":@"",
                             @"Sign":@"CB2E8A4922AF3992A8AE776A4230D9C3E33DD5A0"
                             };

    
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"QUEYMERCAHNTCONFIG" completeBlock:^(id object) {
        //        NSLog(@"objectobject%@",object);
        NSString *str = [object objectForKey:@"BackData"];
        self.arr = [NetWorkTools arrayWithJsonString:str];

        
    }];
    
}





- (void)didWheelBtnClick:(UIButton *)button
{
  
    self.UserLabel.text = @"";
        if ([button.titleLabel.text isEqualToString:@"开始"]) {
            NSString *str = [NSString stringWithFormat:@"%@",temp];
            if ([str isEqualToString:@"0"] || ![num isEqualToString:@"0"]) {
                if ([str isEqualToString:@"0"]) {
                    UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"抽奖次数已达上限" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    
                    [alview show];
                }else if (![num isEqualToString:@"0"]){
                    UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:messange delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    
                    [alview show];
                }
                
            }else{
                [button setTitle:@"停止" forState:UIControlStateNormal];
                
                [button setBackgroundImage:[UIImage imageNamed:@"arrow_tz"] forState:UIControlStateNormal];
                [self.wheel startRotating];
                time   =   [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(actionTime) userInfo:nil repeats:YES];
            }
        
           

        }else{
        
                
                self.view.userInteractionEnabled = NO;
                [button setTitle:@"开始" forState:UIControlStateNormal];
                [button setBackgroundImage:[UIImage imageNamed:@"arrow_ks"] forState:UIControlStateNormal];
                [self.wheel stopRotating];
                [self.wheel getstr:@"" result:^(id result) {
                 NSString *Odds = [NSString stringWithFormat:@"%@",result[@"Odds"]];
                    if ([Odds isEqualToString:@"0"]) {
                        // BUG  选中概率为0项
                        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                        [alview show];
                    }else{
                        self.str = [NSString stringWithFormat:@"%@",result[@"Value"]];
                        NSString *a = [NSString stringWithFormat:@"%@%@",self.str,@"积分"];
                        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                            [time invalidate];
                            self.UserLabel.text = @"恭喜您获得";
                            self.topLabel.text = a;
                            [self netWorkTools];
                            if ([self.str isEqualToString:@"-1"]) {
                                self.topLabel.text = @"谢谢参与";
                            }
                            self.view.userInteractionEnabled = YES;
                        });

                    }
                    
                    
                }];
            
           
        }
    
}

- (void)actionTime{
     NSArray *arr = [ReadLoginData readZhuanPanArr];
    NSInteger i = arc4random() % 8;
    self.topLabel.text = [NSString stringWithFormat:@"%@%@",arr[i][@"Info"],@"积分"];
    if ([arr[i][@"Info"] isEqualToString:@"谢谢参与"]) {
        self.topLabel.text = @"谢谢参与";
    }
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 2000) {
        
       
       
    }
    
}

- (void)refreshPropertyList{
    
    if ([self.wheelButton.titleLabel.text isEqualToString:@"开始"]) {
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        
    }
    
}

- (void)shareNetWork{
    
    NSDictionary *dict = [ReadLoginData readLoginData];
    NSString *userNumber = [dict objectForKey:@"Number"];
    
    NSDictionary *params = @{
                             @"U_Number":userNumber
                             
                             };
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"RESIDUECOUNT" completeBlock:^(id object) {
        NSLog(@"objectobject%@",object);
        NSString *data = object[@"BackData"];
        NSDictionary *dic = [NetWorkTools dictionaryWithJsonString:data];
        NSLog(@"dictdict%@",dic[@"Cha"]);
        temp  =[NSString stringWithFormat:@"%@",dic[@"Cha"]];
        NSString *numA = [NSString stringWithFormat:@"%@",object[@"Id"]];
        num = numA;
         messange= [NSString stringWithFormat:@"%@",object[@"Message"]];
        
//        if ([num isEqualToString:@"0"]) {
//          temp  =[NSString stringWithFormat:@"%@",dic[@"Cha"]];
//            if ([temp isEqualToString:@"0"]) {
//                UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"抽奖次数已达上限" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//                    [alview show];
//            }
//        }else{
//            UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message: [NSString stringWithFormat:@"%@",object[@"Message"]] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//            
//            [alview show];
//            temp = @"0";
//            
//        }
        
        
    }];
    
}
- (void)netWorkTools {
    #warning 体现金额
    NSDictionary *dict = [ReadLoginData readLoginData];
    NSString *userNumber = [dict objectForKey:@"Number"];
    
    NSString *buyPassWord = [dict objectForKey:@"Buy_Password"];
    NSDictionary *params = @{
                             @"UserNumber":userNumber,
                             @"TiXianMoney":self.str,
                             @"BusiPassword":buyPassWord,
                             @"Type":@"1"
                             };
    //   GUANG GAO FEN HONG HUO XIAN JIN TI XIAN
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"GUANGGAOFENHONGHUOXIANJINTIXIAN" completeBlock:^(id object) {
        NSString *str = [object objectForKey:@"Message"];

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:str delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        alert.tag = 2000;
        [alert show];
         }];
}



@end
